package com.softa.web.controller.business;

import com.github.pagehelper.PageInfo;
import com.softa.annotation.CurrentUser;
import com.softa.dao.dto.ProductDTO;
import com.softa.dao.dto.SysUserDTO;
import com.softa.dao.model.Product;
import com.softa.dao.model.SysUser;
import com.softa.service.business.ProductService;
import com.softa.service.business.ProductTypeService;
import com.softa.utils.Constants;
import com.softa.vo.DataGridRequest;
import com.softa.vo.DataGridResponse;
import com.softa.vo.QueryRequest;
import com.softa.web.controller.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by yangl on 2017/7/29.
 * 产品信息
 */
@Controller
@RequestMapping("/product")
public class ProductController extends BaseController {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductTypeService productTypeService;

    @RequestMapping(method = RequestMethod.GET)
    public String list(Model model) {
        return Constants.MENU_MANAGE+"/product/list";
    }

    @RequestMapping(value = "showaddpage", method = RequestMethod.GET)
    public String showaddpage(Model model) {
        model.addAttribute("types", productTypeService.queryAll());
        return Constants.MENU_MANAGE+"/product/add";
    }

    @RequestMapping(value="/listPage",method = RequestMethod.POST)
    @ResponseBody
    public DataGridResponse listPage(DataGridRequest request, QueryRequest queryRequest) {
        PageInfo<ProductDTO> pageResult = productService.queryProductByParam(request, queryRequest);
        return new DataGridResponse(pageResult);
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public String create(@CurrentUser SysUser loginUser, ProductDTO productDTO) {
        String message="录入成功";
        try {
            productDTO.setCreateUser(loginUser.getId());
            productDTO.setUpdateUser(loginUser.getId());
            message = productService.createProduct(productDTO);
        } catch (Exception e) {
            message="录入失败";
            logger.error(e.getMessage());
        }
        return message;
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public String update(@CurrentUser SysUser loginUser, ProductDTO productDTO) {
        String message="修改成功";
        try {
            productDTO.setUpdateUser(loginUser.getId());
            message = productService.updateProduct(productDTO);
        } catch (Exception e) {
            message="修改失败";
            logger.error(e.getMessage());
        }
        return message;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public String delete(@CurrentUser SysUser loginUser, Product product) {
        String message="删除成功";
        try {
            product.setUpdateUser(loginUser.getId());
            productService.deleteProduct(product);
        } catch (Exception e) {
            message="删除失败";
            logger.error(e.getMessage());
        }
        return message;
    }

    @RequestMapping(value = "/changestatus", method = RequestMethod.POST)
    @ResponseBody
    public String changestatus(@CurrentUser SysUser loginUser, Product product) {
        String message="修改成功";
        try {
            product.setUpdateUser(loginUser.getId());
            productService.changeStatus(product);
        } catch (Exception e) {
            message="修改失败";
            logger.error(e.getMessage());
        }
        return message;
    }

}
